package com.atguigu.datasource;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.util.Collector;

import java.nio.charset.StandardCharsets;
import java.util.Properties;

public class Demo01 {
    public static void main(String[] args) throws Exception {
        //1、创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(2);
        //2、获取数据源
        //这里获取kafka数据源
        //创建配置信息，存放连接kafka的数据信息
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "hadoop102:9092,hadoop103:9092,hadoop104:9092");
        properties.setProperty("group.id", "Flink03_Source_Kafka");
        properties.setProperty("auto.offset.reset", "latest");


        DataStreamSource<String> data = env.addSource(new FlinkKafkaConsumer<String>("test", new SimpleStringSchema(StandardCharsets.UTF_8), properties));



        //3、具体处理数据
        SingleOutputStreamOperator<String> result = data.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String value, Collector<String> out) throws Exception {
                String[] s = value.split(" ");
                for (String str : s) {
                    out.collect(str);
                }
            }
        });



        //4、数据输出
        result.print();

        //5、提交执行
        env.execute();


    }
}
